/**
 * author: Maxim Dybarskiy
 * date:   Jun 23, 2011
 * time:   4:09:21 PM
 */
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.File;

public class FileView extends LinearLayout {

    private ImageView image;
    private TextView text;
    
    public FileView(Context context, File file) {
        super(context);
        // init textview
        text = new TextView(context);
        text.setLayoutParams(new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                40));
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        text.setPadding(10, 0, 0, 0);
        text.setTextSize(18);
        text.setText(file.getName());
        // set gray text for locked folder
        if (!file.canRead()) {
            text.setTextColor(Color.parseColor("#777777"));
        }
        // add expand icon if folder
        if (file.isDirectory()) {
            image = new ImageView(context);
            image.setLayoutParams(new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER_VERTICAL | Gravity.LEFT));
            image.setImageResource(R.drawable.collapsed);
            this.addView(image);
        }
        this.addView(text);
    }
    
    public void setExpanded(boolean isExpanded) {
        image.setImageResource(isExpanded ? R.drawable.expanded : R.drawable.collapsed);
        image.invalidate();
    }
}
